﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace Server
{
	public class Auction
	{
		private int id;
		private double minimumPrice;
		private Bid heighestBid;
		private string name;

        private Timer hammertime;
        private int timeleft;
        private object lockobj = new object();

        private AuktionsService auktionsService;
		private bool active = false;

		public Auction(int id, string name, double minimumPrice, AuktionsService auktionsService)
		{
			//Sets the attributes
            this.auktionsService = auktionsService;
			this.id = id;
			this.name = name;
			this.minimumPrice = minimumPrice;
			heighestBid = null;
            active = true;
            timeleft = 17;
            hammertime = new Timer(1000);
            hammertime.Enabled = false;
            hammertime.Elapsed += countdown;
		}

		/// <summary>
		/// Places a bid on the auction and then resetting the countdown timer.
		/// </summary>
		/// <param name="bid"></param>
		/// <param name="client"></param>
		/// <returns></returns>
		public placeBidReturn PlaceBid(double bid, Clienthandler client)
		{
            lock (lockobj)
            {
                if (heighestBid != null)
                {
                    if (bid <= heighestBid.Bid1)
                        return placeBidReturn.BidTooLow;
                }
				if (heighestBid == null || bid > heighestBid.Bid1)
				{
					//Starts the countdown timer
                    hammertime.Enabled = true;
					timeleft = 17;
					Bid newBid = new Bid(bid, client);
					heighestBid = newBid;
				}
                return placeBidReturn.BidOk;
            }
		}

		/// <summary>
		/// Counts down time on the auction
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
        private void countdown(object sender, ElapsedEventArgs e)
        {
            lock (lockobj)
            {
                --timeleft;
                if (timeleft == 7)
                {
					//Sends a broadcast message
                    auktionsService.Broadcast("AuctionUpdate;" + id + ";" + 1);
                }
                else if (timeleft == 2)
                {
					//Sends a broadcast message
                    auktionsService.Broadcast("AuctionUpdate;" + id + ";" + 2);
                }
                else if (timeleft == 0)
                {
					//Stops countdown timer and auction
                    hammertime.Enabled = false;
                    active = false;
					//Sends a broadcast message
                    auktionsService.Broadcast("AuctionClosed;" + id + ";" + heighestBid.Bid1 + ";" + heighestBid.Client.ClientIP);
                }
            }
        }


		public int Id
		{
			get { return id; }
		}

		public Bid HeighestBid
		{
			get
			{
				Bid bid;
				lock (lockobj)
				{
					bid = heighestBid;
				}
				return bid;
			}
		}

		public double MinimumPrice
		{
			get
			{
				double minPrice;
				lock (lockobj)
				{
					minPrice = minimumPrice;
				}
				return minPrice;
			}
		}

		public string Name
		{
			get
			{
				string name;
				lock (lockobj)
				{
					name = this.name;
				}
				return name;
			}
		}

		public bool Active
		{
			get 
			{
				bool isActive;
				lock (lockobj)
				{
					isActive = active;
				}
				return isActive;
			}
		}
	}
}
